

<template>
  <div>
    <Card>
      <Collapse style="margin-bottom:15px;">
        <Panel>查询
          <div slot="content">
            <Form
              ref="formTest"
              :model="formTest"
              label-position="right"
              :label-width="80"
              inline
            >
              <FormItem label="id" prop="id">
                <InputNumber v-model="formTest.id" placeholder=""/>
              </FormItem>
              <FormItem label="姓名" prop="name">
                <Input type="text" v-model="formTest.name" placeholder=""/>
              </FormItem>
              <FormItem label="年龄" prop="age">
                <InputNumber v-model="formTest.age" placeholder=""/>
              </FormItem>
              <FormItem>
                <Button type="primary" @click="handleSubmit('formTest')">查询</Button>
                <Button @click="handleReset('formTest')" style="margin-left: 8px">重置</Button>
              </FormItem>
            </Form>
          </div>
        </Panel>
      </Collapse>
      <ButtonGroup style="margin-bottom: 15px;">
        <Button @click="handleOpenPage('add')">添加</Button>
        <Add v-model="isOpenAddPage" @addRow="addTest"/>
        <Edit v-model="isOpenEditPage" @updateRow="updateTest" :test="editTest"/>
        <Button @click="batchDelete()">删除</Button>
      </ButtonGroup>
      <Table
        ref="tableTest"
        :data="tableData"
        :columns="columns"
        @on-selection-change="onSelectionChange"
        @on-sort-change="onSortChange"
      />
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <Page
            :total="total"
            :current="current"
            :page-size="pageSize"
            @on-page-size-change="changePageSize"
            @on-change="changePage"
            :show-total="true"
            show-sizer
          ></Page>
        </div>
      </div>
      <Button style="margin: 10px 0;" type="primary" @click="exportExcel">导出为Csv文件</Button>
		<Upload
				:format="['doc','docx']"
				:max-size="1024*50"
				:on-exceeded-size="handleMaxSize"
				:on-format-error="uploadFormatError"
				:on-success="(response, file, fileList)=> LoadSuccess(response, file, fileList)"
		        action="/SoJpt/file/fileUpload"
		        :headers="headers">
		        <div style="padding: 20px 0">
		          <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon>
		          <p>Click or drag files here to upload</p>
		        </div>
		      </Upload>

		<a href="/SoJpt/file/download">下载文档</a>
		<a href="/SoJpt/major/toHtml">在线预览</a>
		<a href="/SoJpt/admin/fileLookAndDown/list">测试数据</a>
    </Card>
  </div>
</template>

<script>
import { list, del, batchDelete } from '@/api/test'
import { createFormObj,getToken } from '@/libs/util'

import Add from './add.vue'
import Edit from './edit.vue'

export default {
  name: 'Test',
  components: {
    Add,
    Edit
  },
  data () {
    return {
      formTest: {

        id: null,
        name: '',
        age: null
      },
		 headers: {
		          'Access-Control-Allow-Origin': '*'
		        },
      columns: [
        { type: 'selection', width: 60, align: 'center' },
        { key: 'id', title: '主键', sortable: true },
        { key: 'name', title: '姓名', sortable: true },
        { key: 'age', title: '年龄', sortable: true },
        {
          title: '操作',
          key: 'action',
          width: 250,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      console.log(getToken())
                    }
                  }
                },
                '自定义'
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.editTest = params.row
                      this.handleOpenPage('edit')
                    }
                  }
                },
                '编辑'
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '提示',
                        content: '<p>确定删除吗?</p>',
                        onOk: () => {
                          del({ 'test.id': params.row.id }).then(res => {
                            if (res) {
                              this.tableData.splice(params.index, 1)
                            }
                          })
                        },
                        onCancel: () => {}
                      })
                    }
                  }
                },
                '删除'
              )
            ])
          }
        }
      ],
      tableData: [],
      total: 0,
      current: 1,
      pageSize: 10,
      isOpenAddPage: false,
      isOpenEditPage: false,
      editTest: {},
      selection: []
    }
  },
  methods: {
    handleMaxSize (file) {
		  this.$Message.error({
		    title: '文件大小超限',
		    desc: '文件  ' + file.name + ' 太大，上传文件大小不能超过50M.'
		  })
    },
    uploadFormatError () {
      this.$Message.error('上传类型只能是doc,docx')
    },
    // 文件前端上传成功  后台返回的结果
    LoadSuccess (response, file, fileList) {
      alert(response.toString())
    },
    handleSubmit (name) {
      // 查询按钮事件
      this.getTestList(this.current, this.pageSize)
      this.$Message.success('Success!')
    },
    handleReset (name) {
      // 重置按钮事件
      this.$refs[name].resetFields()
    },
    handleOpenPage (name) {
      // 打开页面事件(添加页,编辑页...)
      if (name === 'add') this.isOpenAddPage = true
      else if (name === 'edit') {
        this.isOpenEditPage = true
      }
    },
    addTest (row) {
      this.tableData.push(row)
      this.getTestList(this.current, this.pageSize) // 用于回显数据
    },
    updateTest (row) {
      this.$set(this.tableData, row._index, row)
    },
    batchDelete () {
      this.$Modal.confirm({
        title: '提示',
        content: '<p>确定删除吗?</p>',
        onOk: () => {
          if (this.selection.length > 0) {
            let ids = ''
            for (var i = 0; i < this.selection.length; i++) {
              ids += ',' + this.selection[i].id
            }
            batchDelete({ ids: ids }).then(res => {
              for (var i = 0; i < this.selection.length; i++) {
                this.tableData = this.tableData.filter(
                  item => item.id !== this.selection[i].id
                )
                this.getTestList(this.current, this.pageSize) // 用于回显数据
              }
            })
          } else {
            this.$Message.error('未选中记录!')
          }
        },
        onCancel: () => {
        }
      })
    },
    getTestList (current, pageSize, paras) {
      let para = { pageNumber: current, pageSize: pageSize }
      if (paras) Object.assign(para, paras)

      list(this.handleRequestParams(para)).then(res => {
        this.tableData = res.list
        this.current = parseInt(res.pageNumber)
        this.pageSize = parseInt(res.pageSize)
        this.total = parseInt(res.totalRow)
      })
    },
    handleRequestParams (para) {
      Object.assign(para, createFormObj(this.formTest, 'test'))
      return para
    },
    exportExcel () {
      this.$refs.tableTest.exportCsv({
        filename: `table-${new Date().valueOf()}.csv`
      })
    },
    changePage (pageNumber) {
      this.getTestList(pageNumber, this.pageSize)
    },
    changePageSize (pageSize) {
      this.getTestList(this.current, pageSize)
    },
    remove (index) {
      this.data6.splice(index, 1)
    },
    onSelectionChange (selection) {
      this.selection = selection
    },
    onSortChange (obj) {
      if (obj.order === 'normal') {
        this.getTestList(1, this.pageSize)
      } else {
        this.getTestList(1, this.pageSize, {
          field: obj.key,
          order: obj.order
        })
      }
    }
  },
  mounted () {
    this.getTestList(this.current, this.pageSize)
  }
}
</script>

<style>
</style>


